﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Interface;
using LY.Common;
using LY.Common.CacheManager;
using LY.Common.Exceptions;
using LY.Entity;
using LY.DataAccess.Repository;
using LY.Entity.PageSearch;
using LY.Entity.Entities;
using LY.Entity.SearchEntity;
using LY.Entity.Jsons;

namespace LY.Business.ServiceImpl
{
    public class SystemService : ISystemService
    {
         SystemRepository _repo;
         public SystemService()
        {
            _repo = new SystemRepository(); 
        }
         /// <summary>
         /// 用户列表
         /// </summary>
         /// <param name="search"></param>
         /// <param name="view"></param>
         /// <returns></returns>
        public PagedList<S_User> QueryUserList(UserSearch search, PageView view)
        {
            return _repo.QueryUserList(search, view);
        }

        //用户实体
        public S_User GetUser(int? id)
        {
            return _repo.GetUser(id);
        }

        //用户登录
        public S_User LoginUser(string Name,string Pwd)
        {
            return _repo.LoginUser(Name,Pwd);
        }

        //保存用户
        public int SaveUser(S_User user)
        {
            
            if (user.ID <= 0)
            {
                //新增
                int ret = _repo.CheckUser(user.LoginName);
                if (ret > 0)
                {
                    return -2;//已存在相同的用户名
                }
                user.Password = LY.Common.Utils.MD5Util.GetMD5_32("123456");
                return (int)_repo.Insert(user);
            }
            else
            {
                int ret = _repo.CheckUser(user.LoginName);
                if (ret >= 2 || ret == 1)
                {
 
                }
                user.RemoveUpdateColumn("LoginName");
                user.RemoveUpdateColumn("Password");
                //编辑
                return (int)_repo.Update(user);
            }
        }

        //删除用户
        public int DeleteUser(int? id)
        {
            return _repo.DeleteUser(id);
        }
    }
}
